package com.maxafaa.contrademo;

import android.graphics.Canvas;
import android.graphics.Bitmap;

public class Animation{
	
	private int m_frame_count;
	private Bitmap[] m_frame;
	private int current_frame;
	private boolean is_increase_frame;
	private boolean is_loop_backward;
	private final long M_DEFAULT_DELAY_PER_FRAME = 100;
	private long m_delay_per_frame;
	private long m_last_frame_time;

	public Animation(Bitmap sprite_bitmap,
					int sprite_set_start_x,
					int sprite_set_start_y,
					int frame_width,
					int frame_height,
					int m_frame_count,
					int distance_beetwen_frames,
					boolean is_vertical_set,
					boolean is_loop_backward,					
					boolean is_increase_frame,
					boolean is_reder_center_of_frame,
					long m_delay_per_frame){
		this.m_frame_count = m_frame_count;		
		this.is_loop_backward = is_loop_backward;		
		this.is_increase_frame = is_increase_frame;
		if(m_delay_per_frame > 0)
			this.m_delay_per_frame = m_delay_per_frame;
		else
			this.m_delay_per_frame = M_DEFAULT_DELAY_PER_FRAME;			
		current_frame = 0;
		m_last_frame_time = System.currentTimeMillis();		
		m_frame = new Bitmap[m_frame_count];
		if(is_vertical_set){
			//next frame located on the botton
			for(int i = 0; i < m_frame_count; i ++){				
				m_frame[i] = Bitmap.createBitmap(sprite_bitmap, sprite_set_start_x, sprite_set_start_y + i*frame_height + i*distance_beetwen_frames, frame_width, frame_height);				
			}
		}else{
			//next frame located on the right
			for(int i = 0; i < m_frame_count; i ++){
				m_frame[i] = Bitmap.createBitmap(sprite_bitmap, sprite_set_start_x + i*frame_width + i*distance_beetwen_frames, sprite_set_start_y, frame_width, frame_height);
			}
		}
	}
	
	public Animation(Bitmap sprite_bitmap, int[] frame_pos_x, int[] frame_pos_y, int frame_width, int frame_height, boolean is_loop_backward){		
		this.is_loop_backward = is_loop_backward;
		this.m_delay_per_frame = M_DEFAULT_DELAY_PER_FRAME;		
		current_frame = 0;
		m_last_frame_time = System.currentTimeMillis();
		is_increase_frame = true;
		m_frame_count = frame_pos_x.length;
		m_frame = new Bitmap[frame_pos_x.length];
		for(int i = 0; i < frame_pos_x.length; i ++){
			m_frame[i] = Bitmap.createBitmap(sprite_bitmap, frame_pos_x[i], frame_pos_y[i], frame_width, frame_height);
		}
	}
	
	public Animation(Bitmap sprite_bitmap, int[] frame_pos_x, int[] frame_pos_y, int[] frame_width, int[] frame_height, boolean is_loop_backward){
		
	}
	
	public void render(Canvas canvas, int render_posx, int render_posy){
		canvas.drawBitmap(m_frame[current_frame], render_posx, render_posy, null);
		long current_time = System.currentTimeMillis();
		if(current_time - m_last_frame_time >= m_delay_per_frame){
			m_last_frame_time = current_time;
			if(is_loop_backward){
				//frame:1,2,3,2,1,2,3,...
				if(is_increase_frame){
					current_frame ++;
					if(current_frame == m_frame_count - 1)
						is_increase_frame = false;
				}else{
					current_frame --;
					if(current_frame == 0)
						is_increase_frame = true;
				}
			}else{
				//frame:1,2,3,1,2,3,1,...
				current_frame ++;
				if(current_frame == m_frame_count)
					current_frame = 0;
			}			
		}		
	}
	
	public void render(Canvas canvas, int render_posx, int render_posy, boolean is_reder_center_of_frame){
		if(is_reder_center_of_frame)
			canvas.drawBitmap(m_frame[current_frame], render_posx - m_frame[current_frame].getWidth()/2, render_posy - m_frame[current_frame].getHeight()/2, null);
		else
			canvas.drawBitmap(m_frame[current_frame], render_posx, render_posy, null);
		long current_time = System.currentTimeMillis();
		if(current_time - m_last_frame_time >= m_delay_per_frame){
			m_last_frame_time = current_time;
			if(is_loop_backward){
				//frame:1,2,3,2,1,2,3,...
				if(is_increase_frame){
					current_frame ++;
					if(current_frame == m_frame_count - 1)
						is_increase_frame = false;
				}else{
					current_frame --;
					if(current_frame == 0)
						is_increase_frame = true;
				}
			}else{
				//frame:1,2,3,1,2,3,1,...
				current_frame ++;
				if(current_frame == m_frame_count)
					current_frame = 0;
			}			
		}		
	}
	
	public void render(Canvas canvas, int render_posx, int render_posy, int pos_x_on_frame, int pos_y_on_frame){
		render_posx = render_posx - pos_x_on_frame;
		render_posy = render_posy - pos_y_on_frame;
		canvas.drawBitmap(m_frame[current_frame], render_posx, render_posy, null);
		long current_time = System.currentTimeMillis();
		if(current_time - m_last_frame_time >= m_delay_per_frame){
			m_last_frame_time = current_time;
			if(is_loop_backward){
				//frame:1,2,3,2,1,2,3,...
				if(is_increase_frame){
					current_frame ++;
					if(current_frame == m_frame_count - 1)
						is_increase_frame = false;
				}else{
					current_frame --;
					if(current_frame == 0)
						is_increase_frame = true;
				}
			}else{
				//frame:1,2,3,1,2,3,1,...
				current_frame ++;
				if(current_frame == m_frame_count)
					current_frame = 0;
			}			
		}		
	}
	
}